<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 3/23/2016
 * Des: mysqli类
 */
defined('INHkMvc') or exit('Access Invalid!');
class hkMysqli{

    private $link;

    public function __construct(){
        if(!extension_loaded('mysqli')){
            LD("mysqli extension is unloaded");
        }
    }

    /**
     * 连接数据库
     * @param $config
     */
    public function connect($config){
        $this->link = @mysqli_connect($config['db_host'], $config['db_user'], $config['db_pwd'],'', $config['db_port'], $config['db_socket']);
        if(!$this->link){
            LD("Failed to connect to MySQL: " . mysqli_connect_error());
        }
        $this->setCharset($config['db_char']);
    }

    /**
     * 更改默认数据库
     * @param $db
     * @return bool
     */
    public function selectDb($db){
        return mysqli_select_db($this->link, $db);
    }

    /**
     * 设置字符集
     * @param string $charset
     * @return bool
     */
    public function setCharset($charset = "utf8"){
        return mysqli_set_charset($this->link, $charset);
    }

    /**
     * 执行Sql
     * @param $sql
     * @return bool|mysqli_result
     */
    public function query($sql){
        return mysqli_query($this->link, $sql);
    }

    /**
     * 返回影响记录行数
     * @return int
     */
    public function affectedRows(){
        return mysqli_affected_rows($this->link);
    }

    /**
     * 返回一行关联数组
     * @param $sql
     * @return array|null
     */
    public function fetchAssoc($sql){
        $result = $this->query($sql);
        if($result===false){
            return false;
        }
        return mysqli_fetch_assoc($result);
    }

    /**
     * 返回一行关联数组
     * @param $sql
     * @return array|null
     */
    public function fetchArray($sql){
        $result = $this->query($sql);
        if($result===false){
            return false;
        }
        return mysqli_fetch_array($result);
    }

    /**
     * 获取所有关联结果集
     * @param $sql
     * @param int $type
     * @return array|null
     */
    public function fetchAll($sql, $type = MYSQLI_ASSOC){
        $result = $this->query($sql);
        if($result===false){
            return false;
        }
        return mysqli_fetch_all($result, $type);
    }

    /**
     * 开启事务
     */
    public function begin(){
        $this->query("START TRANSACTION");
    }

    /**
     * 提交事务
     * @return bool
     */
    public function commit(){
        return mysqli_commit($this->link);
    }

    /**
     * 事务回滚
     * @return bool
     */
    public function rollback(){
        return mysqli_rollback($this->link);
    }

    /**
     * 返回结果集数
     * @param $sql
     * @return int
     */
    public function numRows($sql){
        $result = $this->query($sql);
        if($result===false){
            return false;
        }
        return mysqli_num_rows($result);
    }

    /**
     * 返回插入的id
     * @return int|string
     */
    public function insertId(){
        return mysqli_insert_id($this->link);
    }

    /**
     * 返回当前线程
     * @return int
     */
    public function threadId(){
        return mysqli_thread_id($this->link);
    }

    /**
     * 杀死线程
     * @param $id
     * @return bool
     */
    public function kill($id){
        return mysqli_kill($this->link, $id);
    }

    /**
     * 返回 MySQL 服务器版本
     * @return string
     */
    public function serverInfo(){
        return mysqli_get_server_info($this->link);
    }

    /**
     * 返回错误描述
     * @return string
     */
    public function error(){
        return mysqli_error($this->link);
    }

    /**
     * 关闭链接
     */
    public function close(){
        mysqli_close($this->link);
    }

    public function __destruct(){
        $this->close();
    }

}
